package com.meitianhui.convenitenbuy.module.wallet.presenter;

import android.view.View;

import com.google.gson.Gson;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.base.BaseResponse;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.http.ResponseObserver;
import com.meitianhui.convenitenbuy.main.model.bean.UserToken;
import com.meitianhui.convenitenbuy.module.order.model.bean.EmptyInfo;
import com.meitianhui.convenitenbuy.module.wallet.contract.WalletCardDetailContract;
import com.meitianhui.convenitenbuy.module.wallet.http.WalletApiDataSource;
import com.meitianhui.convenitenbuy.module.wallet.ui.activity.WalletCardDetailActivity;
import com.meitianhui.convenitenbuy.widget.dialog.ShareDialog;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import java.util.HashMap;
import java.util.Map;


/**
 * Created by Administrator on 2017/11/2.
 */

public class WalletCardDetailPresenter implements WalletCardDetailContract.Presenter {

    private WalletCardDetailActivity context;
    private final WalletCardDetailContract.View view;
    private Gson gson;

    public WalletCardDetailPresenter(WalletCardDetailActivity view,
                                     WalletCardDetailActivity context) {
        this.view = view;
        this.context = context;
        gson = new Gson();
    }

    @Override
    public void attachView(Object view) {
    }

    @Override
    public void detachView() {
    }

    @Override
    public void unBindCard(String cardNo) {
        UserToken token = InfoProvider.getUserToken(App.getInstance());
        String userToken = token.getUser_token();
        String service = "finance.consumerPrepayCardUnBind";

        Map<String, String> map = new HashMap<>();
        map.put("member_id", token.getMember_id());
        map.put("card_no", cardNo);
        String jsonParams = gson.toJson(map);

        WalletApiDataSource.getInstance()
                .unBind(userToken, service, jsonParams)
                .subscribe(new ResponseObserver<BaseResponse<EmptyInfo>>(context, true) {
                    @Override
                    public void onSuccess(BaseResponse<EmptyInfo> response) {
                        view.unBindCardBack(true, null);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        view.unBindCardBack(false, msg);
                    }
                });
    }

    @Override
    public void modifyRemark(String input, String cardNo) {
        String userToken = InfoProvider.getUserToken(App.getInstance()).getUser_token();
        String service = "finance.consumerPrepayCardEdit";

        Map<String, String> map = new HashMap<>();
        map.put("card_no", cardNo);
        map.put("remark", input);
        String jsonParams = gson.toJson(map);

        WalletApiDataSource.getInstance()
                .modifyCardRemark(userToken, service, jsonParams)
                .subscribe(new ResponseObserver<BaseResponse<EmptyInfo>>(context, true) {
                    @Override
                    public void onSuccess(BaseResponse<EmptyInfo> response) {
                        view.modifyRemarkBack(true, null);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        view.modifyRemarkBack(false, msg);
                    }
                });
    }

    @Override
    public ShareDialog.OnShareClickListener shareListener(UMImage umImage, String cardNo,
                                                          UMShareListener shareListener) {
        ShareDialog.OnShareClickListener onShareClickListener = new ShareDialog.OnShareClickListener() {
            @Override
            public void FriendCircle(View v) {
                UMWeb web = new UMWeb(Constants.HOST + "h5/c/share/prepaycard?card_no=" + cardNo);
                web.setTitle(context.getResources().getString(R.string.app_name));//标题
                web.setThumb(umImage);  //缩略图
                // web.setDescription("百万优品，免费领回家");//描述

                new ShareAction(context)
                        .setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE)
                        .setCallback(shareListener)
                        .withText(context.getString(R.string.wallet_family_card))
                        .withMedia(web)
                        .share();
                view.dismissDialog();
            }

            @Override
            public void WeChatFriends(View v) {
                UMWeb web = new UMWeb(Constants.HOST + "h5/c/share/prepaycard?card_no=" + cardNo);
                web.setTitle(context.getResources().getString(R.string.app_name));//标题
                web.setThumb(umImage);  //缩略图
                // web.setDescription("百万优品，免费领回家");//描述

                new ShareAction(context)
                        .setPlatform(SHARE_MEDIA.WEIXIN)
                        .withText(context.getString(R.string.wallet_family_card))
                        .setCallback(shareListener)
                        .withMedia(web)
                        .share();
            }

            @Override
            public void QQFriends(View v) {
                UMWeb web = new UMWeb(Constants.HOST + "h5/c/share/prepaycard?card_no=" + cardNo);
                web.setTitle(context.getResources().getString(R.string.app_name));//标题
                web.setThumb(umImage);  //缩略图
                // web.setDescription("百万优品，免费领回家");//描述

                new ShareAction(context)
                        .withText(context.getString(R.string.wallet_family_card))
                        .setPlatform(SHARE_MEDIA.QQ)
                        .setCallback(shareListener)
                        .withMedia(web)
                        .share();
            }

            @Override
            public void QQZone(View v) {
                UMWeb web = new UMWeb(Constants.HOST + "h5/c/share/prepaycard?card_no=" + cardNo);
                web.setTitle(context.getResources().getString(R.string.app_name));//标题
                web.setThumb(umImage);  //缩略图
                // web.setDescription("百万优品，免费领回家");//描述

                new ShareAction(context)
                        .setPlatform(SHARE_MEDIA.QZONE)
                        .setCallback(shareListener)
                        .withMedia(web)
                        .share();
            }

            @Override
            public void WxMini(View v) {
            }
        };

        return onShareClickListener;
    }

}
